package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.service.util.ProdCateUtil;
import com.iwhalecloud.bss.kite.dataservice.cache.ProductCache;
import org.springframework.beans.factory.annotation.Autowired;

import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteObjectUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.ActionType;
import com.iwhalecloud.bss.kite.manager.enums.InstType;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInstFunc;
import com.iwhalecloud.bss.kite.service.attr.data.CustAttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;

/**
 * 是否实名制后的默认通信计划属性处理器
 */
public class DefaultCommunicationPlanAttrHandler extends AbstractAttrHandler<KiteProdInstFunc, CustAttrData> {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    private static final String YWCJ = "200001679";

    @Override
    public void after(KiteAttr attr, KiteProdInstFunc inst, CustAttrData data) {
        super.after(attr, inst, data);
        KiteInst kiteInst = querySceneInstService
            .getKiteInst(data.getCustId(), InstType.PROD_INST, inst.getAccProdInstId());
        if (!KiteObjectUtils.isEmpty(kiteInst) && !KiteObjectUtils.isEmpty(kiteInst.getAttr(YWCJ))) {
            KiteAttr kiteAttr = kiteInst.getAttr(YWCJ);
            if (KiteStringUtils.equals("100001822", kiteAttr.getValue())) { //B2B2C
                attr.setIsNull(KeyConsts.IFTRUE_F);
            }
            if (KiteStringUtils.equals("100001821", kiteAttr.getValue())) { //B2B
                attr.setIsNull(KeyConsts.IFTRUE_T);
            }
        }
    }

    /**
     * NB产品、5G连接产品和3/4G连接产品：业务场景为B2B2C时，只能有一个实名制后默认通信计划
     */
    @Override
    public void before(KiteAttr attr, KiteProdInstFunc inst, String value, CustAttrData data) {
        super.before(attr, inst, value, data);
        if (KiteStringUtils.equals(value, "1_0000107")) {
            return;
        }
        KiteInst prodInst = querySceneInstService.getKiteInst(data.getCustId(), InstType.PROD_INST, inst.getAccProdInstId());
        if (!KiteStringUtils
            .equalsAny(prodInst.getSpecId(), ProdCateUtil.getMainMemberProdIdForArr(ProdCateUtil.DZNB_PROD, ProdCateUtil.DZ4G_PROD, ProdCateUtil.DZ5G_PROD, ProdCateUtil.GIFT_DZ4G_PROD, ProdCateUtil.GIFT_DZ5G_PROD))) {
            return;
        }
        KiteAttr kiteAttr = prodInst.getAttr(YWCJ);
        if (Objects.nonNull(kiteAttr) && !"100001822".equals(kiteAttr.getValue())) { //B2B2C
            return;
        }
        List<KiteProdInstFunc> prodInstFuncs = querySceneInstService.listProdInstFuncs(data.getCustId(), prodInst.getInstId());
        List<KiteProdInstFunc> planFuncs = prodInstFuncs.stream()
            .filter(func -> ActionType.A.equals(func.getActionType())
                && KiteStringUtils.equalsAny(ProductCache.get(func.getSpecId()).getProdNbr(), "300000151", LocalKeyConsts.FUNC_PROD_ID_YF5GTX)
                && !KiteStringUtils.equals(inst.getProdInstId(), func.getInstId()))
            .collect(Collectors.toList());
        Boolean isHasAfterRealNameDefaultPlan = false;
        for (KiteProdInstFunc planFunc : planFuncs) {
            KiteAttr afterRealNameDefaultPlan = planFunc.getAttr("200004023");
            if (KiteStringUtils.equals(afterRealNameDefaultPlan.getValue(), "1_0000106")) {
                isHasAfterRealNameDefaultPlan = true;
                break;
            }
        }
        if (isHasAfterRealNameDefaultPlan) {
            ErrorConsts.BUSI_COMMON_ERROR.throwOut("业务场景为B2B2C时，只能有一个实名制后默认通信计划");
        }
    }
}
